package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.service.DataMaterialSingleHistoryService;
import com.uwlaser.service.dto.DataMaterialSingleHistoryDto;
import com.uwlaser.service.dto.DataMaterialSingleHistoryQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "物料单体码-历史管理")
@RequestMapping("/api/dataMaterialSingleHistory")
public class DataMaterialSingleHistoryController {

    private final DataMaterialSingleHistoryService dataMaterialSingleHistoryService;

    @GetMapping
    @Log("查询物料单体码-历史")
    @ApiOperation("查询物料单体码-历史")
    @PreAuthorize("@el.check('dataMaterialSingleHistory:list','dataMain:list')")
    public ResponseEntity query(DataMaterialSingleHistoryQueryParam query, Pageable pageable){
        return new ResponseEntity<>(dataMaterialSingleHistoryService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增物料单体码-历史")
    @ApiOperation("新增物料单体码-历史")
    @PreAuthorize("@el.check('dataMaterialSingleHistory:add')")
    public ResponseEntity create(@Validated @RequestBody DataMaterialSingleHistoryDto resources){
        return new ResponseEntity<>(dataMaterialSingleHistoryService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改物料单体码-历史")
    @ApiOperation("修改物料单体码-历史")
    @PreAuthorize("@el.check('dataMaterialSingleHistory:edit')")
    public ResponseEntity update(@Validated @RequestBody DataMaterialSingleHistoryDto resources){
        dataMaterialSingleHistoryService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除物料单体码-历史")
    @ApiOperation("删除物料单体码-历史")
    @PreAuthorize("@el.check('dataMaterialSingleHistory:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        dataMaterialSingleHistoryService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/recover")
    @Log("恢复物料单体码-历史")
    public ResponseEntity recover(@Validated @RequestBody List<DataMaterialSingleHistoryDto> resources){
        return new ResponseEntity<>(dataMaterialSingleHistoryService.recover(resources),HttpStatus.CREATED);
    }

    /*
    @Log("导出物料单体码-历史")
    @ApiOperation("导出物料单体码-历史")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dataMaterialSingleHistory:list')")
    public void download(HttpServletResponse response, DataMaterialSingleHistoryQueryParam query) throws IOException {
        dataMaterialSingleHistoryService.download(dataMaterialSingleHistoryService.queryAll(query), response);
    }*/

}
